*Analyses performance of DMSPE combination forecasts based on individual forecasts with PTV constraints 
set more 1


do ptvprepare

if $freq==4{
	gen double theta=$theta^40 if yy==19551
	replace theta=theta[_n-1]/$theta if yy>19551
	local n=15
}

if $freq==12{
	gen double theta=$theta^120 if yy==195501
	replace theta=theta[_n-1]/$theta if yy>195501
	local n=14
}

sum time		
local tstart=r(min)+1
local tlastpred=r(max)

gen double ptvphitotal=0
gen double ptvsphitotal=0
gen double wptvphitotal=0
gen double wptvsphitotal=0


*Produce individual out-of-sample forecasts
forvalues v=1/`n'{
	di `v'
	qui{
	gen double ptvthetafxx`v'=0
	gen double ptvphifxx`v'=0
	gen ptvdfxx`v'=.
	gen double ptvsthetafxx`v'=0
	gen double ptvsphifxx`v'=0
	gen ptvsdfxx`v'=.
	
	gen double wptvthetafxx`v'=0
	gen double wptvphifxx`v'=0
	gen wptvdfxx`v'=.
	gen double wptvsthetafxx`v'=0
	gen double wptvsphifxx`v'=0
	gen wptvsdfxx`v'=.


	
	forvalues i=`tstart'/`tlastpred'{
			
		
			
		capture replace ptvthetafxx`v'=ptvthetafxx`v'[_n-1]+(ptvfxx`v'-lnexret)^2*theta if time==`i' & theta!=.
		capture replace ptvphifxx`v'=1/(ptvthetafxx`v'/theta) if time==`i' & theta!=.
		capture replace ptvphitotal=ptvphitotal+ptvphifxx`v' if time==`i'
		capture replace ptvdfxx`v'=ptvfxx`v'*ptvphifxx`v'[_n-1] if time==`i'
	
	
		*weighted least squares

		capture replace wptvthetafxx`v'=wptvthetafxx`v'[_n-1]+(wptvfxx`v'-lnexret)^2*theta if time==`i' & theta!=.
		capture replace wptvphifxx`v'=1/(wptvthetafxx`v'/theta) if time==`i' & theta!=.
		capture replace wptvphitotal=wptvphitotal+wptvphifxx`v' if time==`i'
		capture replace wptvdfxx`v'=wptvfxx`v'*ptvphifxx`v'[_n-1] if time==`i'
	
		
		*with simple returns
		
		capture replace ptvsthetafxx`v'=ptvsthetafxx`v'[_n-1]+(ptvsfxx`v'-exret)^2*theta if time==`i' & theta!=.
		capture replace ptvsphifxx`v'=1/(ptvsthetafxx`v'/theta) if time==`i' & theta!=.
		capture replace ptvsphitotal=ptvsphitotal+ptvsphifxx`v' if time==`i'
		capture replace ptvsdfxx`v'=ptvsfxx`v'*ptvsphifxx`v'[_n-1] if time==`i'

		*weighted least squares

		capture replace wptvsthetafxx`v'=wptvsthetafxx`v'[_n-1]+(wptvsfxx`v'-exret)^2*theta if time==`i' & theta!=.
		capture replace wptvsphifxx`v'=1/(wptvsthetafxx`v'/theta) if time==`i' & theta!=.
		capture replace wptvsphitotal=wptvsphitotal+wptvsphifxx`v' if time==`i'
		capture replace wptvsdfxx`v'=wptvsfxx`v'*wptvsphifxx`v'[_n-1] if time==`i'
	
	
		
	
		}
		
		
		
	}
}

*Generate mean forecasts
capture drop pred spred
egen pred=rowtotal(ptvdfxx*)
replace pred=pred/ptvphitotal[_n-1]

egen spred=rowtotal(ptvsdfxx*)
replace spred=spred/ptvsphitotal[_n-1]




do evaluation


gen what="PTV dmspe$theta" in 1
local s="ResPTVdmspe$theta$c$h$sep$freq"
local ds="DataPTVdmspe$theta$c$h$sep$freq"



export excel what oos* cduoptsmodelmean* cduoptbuyhold*  using results.xlsx in 1/30, sheetmod sheet("`s'") cell(A1) first(var)
export excel yy* pred lnexret modelmean using results.xlsx if yyy>=$firstpred, sheetmod sheet("`s'") cell(A10) first(var)


*with weighted LS
capture drop pred spred
egen pred=rowtotal(wptvdfxx*)
replace pred=pred/wptvphitotal[_n-1]


egen spred=rowtotal(wptvsdfxx*)
replace spred=spred/wptvsphitotal[_n-1]

do evaluation


capture drop what
gen what="wPTV dmspe$theta" in 1
local s="wResPTVdmspe$theta$c$h$sep$freq"

export excel what oos* cduoptsmodelmean* cduoptbuyhold*  using results.xlsx in 1/30, sheetmod sheet("`s'") cell(A1) first(var)


